package com.example.adapter_sdk.quick;

import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.ijk.widget.IjkView;
import com.example.loadimg_sdk.ImgLoad;


public class BaseViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View>sparseArray=new SparseArray<>();

    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);
    }

//    查找控件
    private <V extends View> V find(int id){
        return itemView.findViewById(id);
    }

    public <V extends View> V getView(int id){
        V v;
        //获取当前holder下已经findviewbyid的子控件
        v= (V) sparseArray.get(id);
        if (v!=null){
            return v;
        }
        //根据id查询子控件
        v = find(id);
        if (v!=null)
            sparseArray.put(id,v);
        return v;
    }

    //设置要显示的字符串方法
    public void setText(int id,String content){
        if (TextUtils.isEmpty(content))
            return;
        TextView textView = getView(id);
        if (textView == null)
            return;
        textView.setText(content);
    }

    //设置控件显示隐藏
    public void setVisibility(int id,boolean visibilityFlag){
        View view = getView(id);
        if (view == null)
            return;
        view.setVisibility(visibilityFlag?View.VISIBLE:View.GONE);
    }

    //设置ImageView显示网络图片
    public void setImage(int id,String path){
        if (TextUtils.isEmpty(path))
            return;
        ImageView imageView = getView(id);
        if (imageView == null)
            return;
        //调用封装完成的glide加载网络图片
        ImgLoad.loadMatchImg(path,imageView);
    }
    public void setCircleImage(int id,String path){
        if (TextUtils.isEmpty(path))
            return;
        ImageView imageView = getView(id);
        if (imageView == null)
            return;
        //调用封装完成的glide加载网络图片
        ImgLoad.loadCircleImg(path,imageView);
    }
    public void setIjk(int id,String url,String path){
        if (TextUtils.isEmpty(path)&&TextUtils.isEmpty(url))
            return;
        IjkView ijkView = getView(id);
        if (ijkView == null)
            return;
        ijkView.setImgUrl(path);
        ijkView.setPlayUrl(url);
    }
}
